package com.xyf.sys.service.impl;

import com.xyf.sys.domain.Role;
import com.xyf.sys.domain.RoleDept;
import com.xyf.sys.mapper.RoleDeptMapper;
import com.xyf.sys.mapper.RoleMapper;
import com.xyf.sys.service.IRoleDeptService;
import com.xyf.sys.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author R-H
 * @Date 2024/2/201831
 * @Description
 */
@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper mapper;
    @Autowired
    private IRoleDeptService roleDeptService;
    @Autowired
    private RoleDeptMapper roleDeptMapper;


    @Override
    public List<Role> getAllRoleList() {
        return mapper.selectAll();
    }

    @Override
    public Role getRole(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int authDataScope(Long roleId, Long[] deptIds) {
        // 删除角色与部门关联
        roleDeptMapper.deleteRoleDeptByRoleId(roleId);
        // 新增角色和部门信息（数据权限）
        return insertRoleDept(roleId, deptIds);
    }

    public int insertRoleDept(Long roleId, Long[] deptIds) {
        int rows = 1;
        // 新增角色与部门（数据权限）管理
        List<RoleDept> list = new ArrayList<>();
        for (Long deptId : deptIds) {
            RoleDept rd = new RoleDept();
            rd.setRoleId(roleId);
            rd.setDeptId(deptId);
            list.add(rd);
        }
        if (list.size() > 0) {
            rows = roleDeptMapper.batchRoleDept(list);
        }
        return rows;
    }
}
